From 1a2c608279791f722de31b7e6b60821e73e8a25f Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Wed, 25 Nov 2009 14:19:50 +0000 Subject: [PATCH] libxenlight: move logging macros to the public header This patch moves the logging macros to the public header so that they can be reused by the client of the library. It also refactors the code to create the qemu logfile into a generic function that can be reused to create generic xen logfiles under /var/log/xen. Finally xl is changed to log to file when running in background. Signed-off-by: Stefano Stabellini --- tools/libxl/libxl.c | 23 ++++------------------- tools/libxl/libxl.h | 22 +++++++++++++++++++++- tools/libxl/libxl_internal.h | 21 --------------------- tools/libxl/libxl_utils.c | 27 +++++++++++++++++++++++++++ tools/libxl/libxl_utils.h | 1 + tools/libxl/xl.c | 22 +++++++++++++++++++++- 6 files changed, 74 insertions(+), 42 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index da2d1dfce1..825363f1dd 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -765,10 +764,9 @@ int libxl_create_device_model(struct libxl_ctx *ctx, libxl_device_nic *vifs, int num_vifs, libxl_device_model_starting **starting_r) { - char *path, *logfile, *logfile_new; - struct stat stat_buf; + char *path, *logfile; int logfile_w, null; - int i, rc; + int rc; char **args; struct libxl_spawn_starting buf_spawn, *for_spawn; @@ -789,22 +787,9 @@ int libxl_create_device_model(struct libxl_ctx *ctx, path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid); xs_mkdir(ctx->xsh, XBT_NULL, path); - logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name); - if (stat(logfile, &stat_buf) == 0) { - /* file exists, rotate */ - logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", info->dom_name); - unlink(logfile); - for (i = 9; i > 0; i--) { - logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i); - logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", info->dom_name, i + 1); - rename(logfile, logfile_new); - } - logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name); - logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", info->dom_name); - rename(logfile, logfile_new); - } - logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", info->dom_name); + libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile); logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644); + free(logfile); null = open("/dev/null", O_RDONLY); if (starting_r) { diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index b568bc4863..4021b015f1 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -23,9 +23,10 @@ #include "xen_uuid.h" +#define XL_LOGGING_ENABLED + typedef void (*libxl_log_callback)(void *userdata, int loglevel, const char *file, int line, const char *func, char *s); - struct libxl_dominfo { xen_uuid_t uuid[16]; uint32_t domid; @@ -224,6 +225,25 @@ typedef struct { #define ERROR_NOMEM (-1032) #define ERROR_INVAL (-1245) +/* logging */ +void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al); +void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...); + +#ifdef XL_LOGGING_ENABLED +#define XL_LOG(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a) +#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a) +#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...) xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a) +#else +#define XL_LOG(ctx, loglevel, _f, _a...) +#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) +#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...) +#endif + +#define XL_LOG_DEBUG 3 +#define XL_LOG_INFO 2 +#define XL_LOG_WARNING 1 +#define XL_LOG_ERROR 0 + /* context functions */ int libxl_ctx_init(struct libxl_ctx *ctx); int libxl_ctx_free(struct libxl_ctx *ctx); diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 8363c94532..1c5a2de43d 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -33,27 +33,6 @@ #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -#define XL_LOGGING_ENABLED - -#ifdef XL_LOGGING_ENABLED -#define XL_LOG(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, -1, __FILE__, __LINE__, __func__, _f, ##_a) -#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) xl_log(ctx, loglevel, errno, __FILE__, __LINE__, __func__, _f, ##_a) -#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...) xl_log(ctx, loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a) -#else -#define XL_LOG(ctx, loglevel, _f, _a...) -#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...) -#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...) -#endif - -#define XL_LOG_DEBUG 3 -#define XL_LOG_INFO 2 -#define XL_LOG_WARNING 1 -#define XL_LOG_ERROR 0 - -void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, va_list al); -void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char *file, int line, const char *func, char *fmt, ...); - typedef enum { DEVICE_VIF, DEVICE_VBD, diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c index 358f735b4f..b42bfc6991 100644 --- a/tools/libxl/libxl_utils.c +++ b/tools/libxl/libxl_utils.c @@ -23,6 +23,9 @@ #include #include #include +#include +#include +#include #include "libxl_utils.h" #include "libxl_internal.h" @@ -187,3 +190,27 @@ int libxl_is_stubdom(struct libxl_ctx *ctx, int domid) return 0; } +int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name) +{ + struct stat stat_buf; + char *logfile, *logfile_new; + int i; + + logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name); + if (stat(logfile, &stat_buf) == 0) { + /* file exists, rotate */ + logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.10", name); + unlink(logfile); + for (i = 9; i > 0; i--) { + logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i); + logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i + 1); + rename(logfile, logfile_new); + } + logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name); + logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name); + rename(logfile, logfile_new); + } + *full_name = strdup(logfile); + return 0; +} + diff --git a/tools/libxl/libxl_utils.h b/tools/libxl/libxl_utils.h index e4e487cb23..cf57b0febb 100644 --- a/tools/libxl/libxl_utils.h +++ b/tools/libxl/libxl_utils.h @@ -31,6 +31,7 @@ char *libxl_uuid_to_string(struct libxl_ctx *ctx, xen_uuid_t *uuid); int libxl_param_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid); int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid); int libxl_is_stubdom(struct libxl_ctx *ctx, int domid); +int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name); #endif diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c index 4c74660951..4056e13c45 100644 --- a/tools/libxl/xl.c +++ b/tools/libxl/xl.c @@ -35,9 +35,14 @@ #include "libxl.h" #include "libxl_utils.h" +int logfile = 2; + void log_callback(void *userdata, int loglevel, const char *file, int line, const char *func, char *s) { - fprintf(stderr, "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s); + char str[1024]; + + snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s); + write(logfile, str, strlen(str)); } static void printf_info(libxl_domain_create_info *c_info, @@ -640,9 +645,18 @@ start: libxl_domain_unpause(&ctx, domid); if (need_daemon) { + char *fullname, *name; + + asprintf(&name, "xl-%s", info1.name); + libxl_create_logfile(&ctx, name, &fullname); + logfile = open(fullname, O_WRONLY|O_CREAT, 0644); + free(fullname); + free(name); + daemon(0, 0); need_daemon = 0; } + XL_LOG(&ctx, XL_LOG_DEBUG, "Waiting for domain %s (domid %d) to die", info1.name, domid); libxl_wait_for_domain_death(&ctx, domid, &fd); while (1) { @@ -658,17 +672,23 @@ start: if (!ret) continue; if (libxl_is_domain_dead(&ctx, domid, &info)) { + XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d is dead", domid); if (info.crashed || info.dying || (info.shutdown && (info.shutdown_reason != SHUTDOWN_suspend))) { + XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d needs to be clean: destroying the domain", domid); libxl_domain_destroy(&ctx, domid, 0); if (info.shutdown && (info.shutdown_reason == SHUTDOWN_reboot)) { libxl_ctx_free(&ctx); + XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Rebooting now"); goto start; } + XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Exiting now"); } + XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d does not need to be clean, exiting now", domid); exit(0); } } + close(logfile); for (i = 0; i < num_vifs; i++) { free(vifs[i].smac); free(vifs[i].ifname); -- 2.30.2